/*
 * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xdlink.component.channel;

/**
 * 数据接收器，用于通信通道定期地接收数据
 *
 * @author xuexiang
 * @since 2018/8/14 下午10:19
 */
public interface IDataReceiver {

    /**
     * @return 获取接收数据的通信通道
     */
    IChannel getChannel();

    /**
     * 开始接收工作
     */
    void startWork();

    /**
     * 在尝试接收数据前
     *
     * @throws Exception
     */
    void beforeAcceptData() throws Exception;

    /**
     * @return 返回接收数据的间期
     */
    long getAcceptInterval();

    /**
     * 尝试接收数据
     *
     * @throws Exception
     */
    void acceptData() throws Exception;

    /**
     * 接收到数据
     *
     * @param data      读取数据的缓存块
     * @param available 接收到数据的长度
     */
    void onDataReceived(byte[] data, int available);

    /**
     * 结束接收工作
     */
    void finishWork();

}
